package beuthTaskList;

import java.util.Collections;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.beuthTaskList.R;

public class TaskList extends Activity  {

	public TaskModel has = TaskModel.getInstance();
	
	public Spinner spinnerSortTypeSelection ;
	
	public ListView listViewTaskListView ;
	

	//Start of user code protectedActivityAttributesTaskList
	private TaskIO dataIO = new TaskIO();
	//End of user code	

	private ArrayAdapter<Task> taskListViewAdapter;

	private void sortTasks(SortType sortType) {
		//Start of user code protectedOperationsortTasks
		Collections.sort(has.getAdministrates(), new TaskComperator(sortType));
		if(taskListViewAdapter != null) {
			taskListViewAdapter.notifyDataSetChanged();
		}
		//End of user code
	}

	


 	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tasklist);

			spinnerSortTypeSelection = (Spinner) findViewById(R.id.spinner_sortTypeSelection);  
			listViewTaskListView = (ListView) findViewById(R.id.listview_taskListView);  

			spinnerSortTypeSelection.setAdapter(new ArrayAdapter<SortType>(this, android.R.layout.simple_dropdown_item_1line, SortType.values()));
			spinnerSortTypeSelection.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> aView, View view,	int arg2, long arg3) {
					//Start of user code protectedOnItemSelectedspinnerSortTypeSelection
					SortType selectedSortType = (SortType) spinnerSortTypeSelection.getSelectedItem();
					sortTasks(selectedSortType);		
					//End of user code			
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					//Start of user code protectedOnNothingSelectedspinnerSortTypeSelection
					//End of user code
				}
				//Start of user code protectedOnItemSelectedListenerspinnerSortTypeSelection
				//End of user code
			});

			taskListViewAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_1,
			has.getAdministrates());
			listViewTaskListView.setAdapter(taskListViewAdapter);
			listViewTaskListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
					//Start of user code protectedOnItemClicklistViewTaskListView
					Intent i = new Intent(view.getContext(), TaskCreateOrEdit.class);
					Bundle b = new Bundle();
					b.putInt("EditTaskNumber", position);
					i.putExtras(b);
					startActivity(i);	
					//End of user code			
				}
			//Start of user code protectedOnItemClickListenerlistViewTaskListView
			//End of user code	
			});

		//Start of user code protectedOnCreateTaskList
		if (has.getAdministrates().size() <= 0) {	
			try {
				has.getAdministrates().clear();
				for (Task task : dataIO.readAllTask()) {
					has.getAdministrates().add(task);
				}
				taskListViewAdapter.notifyDataSetChanged();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}	
		//End of user code
	}

 	@Override
    protected void onResume() {
    	super.onResume();
		//Start of user code protectedOnResumeTaskList
		//End of user code
    }

 	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_beuthtasklist, menu);
        return true;
    }

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
			case R.id.m_taskCreateOrEdit:
				i = new Intent(this, TaskCreateOrEdit.class);
				startActivity(i);        			
				break;
			case R.id.m_taskList:
				i = new Intent(this, TaskList.class);
				startActivity(i);        			
				break;
      		default:
				break;
      	}
      return true;
    } 


//Start of user code protectedActivityCodeTaskList
//End of user code

public TaskModel getHas() {
	return this.has;
}

public void setHas(TaskModel has) {
		this.has = has;
}	

} 
